package com.baijiayun.liveuibase.widgets.popupwindow

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.baijiayun.livecore.utils.DisplayUtils
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.utils.BaseUIUtils
import com.baijiayun.liveuibase.utils.ThemeDataUtil
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder
import com.baijiayun.liveuibase.utils.drawable.StateListDrawableBuilder

/**
 * Created by pengwenjian on 2021/07/23
 * Describe: 选择window
 */
class BaseSelectWindow(
    context: Context,
    private val source: List<String>,
    private val selectedIndex: Int? = -1,
    private val isShowDiv: Boolean? = false,
    private val isRead: Boolean? = false
) : BaseAutoArrangePopupWindow(context) {
    var itemClickListener: OnItemClickListener? = null

    init {
        val container = LinearLayout(context)
        container.orientation = LinearLayout.VERTICAL
        setDirectionMode(HORIZONTAL)
        container.background = DrawableBuilder()
            .cornerRadius(context.resources.getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
            .strokeColor(ContextCompat.getColor(context, R.color.base_bg_stroke_10))
            .strokeWidth(1)
            .solidColor(
                ThemeDataUtil.getColorFromThemeConfigByAttrId(
                    context,
                    if (isRead == true) {
                        R.attr.base_theme_window_bg_color
                    } else {
                        R.attr.base_theme_brand_container_color
                    }
                )
            )
            .build()
        BaseUIUtils.setRoundCorner(
            container,
            context.resources.getDimension(R.dimen.bjy_base_common_bg_radius)
        )
        generateViews(container)
        createView(container, false)
    }

    private fun generateViews(container: LinearLayout) {
        for ((index,text) in source.withIndex()) {
            val textView = TextView(context)
            textView.id = View.generateViewId()
            if (isRead != true) {
                if (index == selectedIndex) {
                    textView.setTextColor(
                        ThemeDataUtil.getColorFromThemeConfigByAttrId(
                            context,
                            R.attr.base_theme_live_product_color
                        )
                    )
                } else {
                    textView.setTextColor(
                        ThemeDataUtil.getColorFromThemeConfigByAttrId(
                            context,
                            R.attr.base_theme_window_main_text_color
                        )
                    )
                }
                val drawable = DrawableBuilder()
                    .solidColor(
                        ContextCompat.getColor(context, R.color.base_main_color_layer_20)
                    )
                    .build()
                textView.background = StateListDrawableBuilder()
                    .normal(DrawableBuilder().build())
                    .pressed(drawable)
                    .build()
                textView.gravity = Gravity.CENTER
            } else {
                textView.setTextColor(
                    ThemeDataUtil.getColorFromThemeConfigByAttrId(
                        context,
                        R.attr.base_theme_window_assistant_text_color
                    )
                )
                textView.gravity = Gravity.START or Gravity.CENTER_VERTICAL
            }
            val topPadding = DisplayUtils.dip2px(context, 4f)
            textView.setPadding(2 * topPadding, topPadding, 2 * topPadding, topPadding)
            textView.text = text
            textView.setOnClickListener { itemClickListener?.onItemClick(it,index) }
            container.addView(textView,LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            ))
            if (isShowDiv == true && index < source.size - 1) {
                val div = View(context)
                div.setBackgroundColor(
                    ContextCompat.getColor(
                        context,
                        R.color.base_divider_line_other
                    )
                )
                container.addView(
                    div, LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        DisplayUtils.dip2px(context, 1f)
                    )
                )
            }
        }
    }

    override fun initViewDataHashMap() {
        // do nothing
    }

    override fun show(anchor: View?) {
        isFocusable = true
        isOutsideTouchable = true
        showWithViewOfDirection(anchor, LEFT_TOP)
    }

    fun show(anchor: View?, direction: Int = LEFT_TOP) {
        isFocusable = true
        isOutsideTouchable = true
        showWithViewOfDirection(anchor, direction)
    }

    interface OnItemClickListener {
        fun onItemClick(view: View,position: Int)
    }
}